package edu.tyut.game.ui.learn

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import edu.tyut.game.databinding.FragmentLearnBinding
import edu.tyut.game.utils.LogUtils

class LearnFragment : Fragment() {

    private val tag = "LearnFragment"

    private var _binding: FragmentLearnBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    val viewModel by lazy {
        ViewModelProvider(this)[LearnViewModel::class.java]
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentLearnBinding.inflate(inflater, container, false)
        val root: View = binding.root

        viewModel.res.observe(viewLifecycleOwner){ res ->
            LogUtils.d(tag, res.getOrNull() ?: "null")
        }

        // 搜索框
        // 将键盘回车设置为搜索
        binding.search.imeOptions = EditorInfo.IME_ACTION_SEARCH
        binding.search.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                val content = query ?: "床前明月光"
                LogUtils.d(tag, content)
                viewModel.getSearchPoemListByContent(content)
                return false
            }
            override fun onQueryTextChange(newText: String?): Boolean {
                LogUtils.d(tag, newText ?: "床前明月光")
                return false
            }
        })
        // 设置PoemList 执子之手
        viewModel.searchPoemList.observe(viewLifecycleOwner){ result ->
            val poemList = result.getOrNull()
            poemList?.apply {
                val adapter = PoemAdapter(this@LearnFragment.requireContext(), this, this@LearnFragment)
                binding.poemList.adapter = adapter
            }
        }


        // 获取诗词朝代
        viewModel.getDynastiesList()
        // 布局管理器
        val layoutManager = LinearLayoutManager(this.requireContext())
        layoutManager.orientation = RecyclerView.HORIZONTAL
        binding.dynastiesList.layoutManager = layoutManager

        viewModel.dynastiesList.observe(viewLifecycleOwner){ result ->
            val dynasties = result.getOrNull()
            dynasties?.let { res ->
                // 推荐模块暂时比较难搞，放一放
                // plus("推荐")
                // 去掉引号
                val mutableList = res.toMutableList()
                mutableList.add("推荐")
                val adapter = DynastiesAdapter(this@LearnFragment.requireContext(), mutableList, viewModel)
                binding.dynastiesList.adapter = adapter
                viewModel.getPoemListByDynasty(mutableList[0])
            }
        }
        // 获取诗词列表
        // 布局管理器
        val gridLayoutManager = GridLayoutManager(this.requireContext(), 3, RecyclerView.VERTICAL, false)
        gridLayoutManager.orientation = RecyclerView.VERTICAL
        binding.poemList.layoutManager = gridLayoutManager

        viewModel.poemList.observe(viewLifecycleOwner){ result ->
            val pair = result.getOrNull()
            pair?.apply {
                LogUtils.d(tag, "获取数据中...")
                // LogUtils.d(tag, this.toString())
                val adapter = PoemAdapter(this@LearnFragment.requireContext(), this, this@LearnFragment)
                binding.poemList.adapter = adapter
            }
        }
        // 获取TOP诗词
        viewModel.resTop.observe(viewLifecycleOwner){ res ->
            val poems = res.getOrNull()
            poems?.let {
                LogUtils.d(tag, "获取数据中...")
                // LogUtils.d(tag, this.toString())
                val adapter = PoemAdapter(this@LearnFragment.requireContext(), it, this@LearnFragment)
                binding.poemList.adapter = adapter
            }
        }


        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}